package com.victor.zjbj_android.mod.zhongxin;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.blankj.utilcode.util.ToastUtils;
import com.victor.zjbj_android.R;
import com.victor.zjbj_android.base.BaseActivity;
import com.victor.zjbj_android.bean.RequestBean;
import com.victor.zjbj_android.utils.FastJsonTools;
import com.victor.zjbj_android.utils.LoginUtils;
import com.victor.zjbj_android.utils.MainUtils;
import com.victor.zjbj_android.utils.ZjbjManager;
import com.victor.zjbj_android.utils.retrofit.BaseSubscriber;
import com.victor.zjbj_android.utils.retrofit.ExceptionHandle;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created by liuwei on 2018/4/19 2018/4/21.
 */

public class XiugaiMimaActivity extends BaseActivity {
    private EditText et_lastpassword,et_newpassword,et_newpassword1;
    private Button btn_commit;

    @Override
    protected int getResourcesId() {
        return R.layout.activity_xiugaimima;
    }

    @Override
    protected void init(Bundle savedInstanceState) {
        btn_commit=findViewById(R.id.btn_commit);
        et_lastpassword=findViewById(R.id.et_lastpassword);
        et_newpassword=findViewById(R.id.et_newpassword);
        et_newpassword1=findViewById(R.id.et_newpassword1);
        findViewById(R.id.iv_back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        btn_commit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(et_lastpassword.getText().toString().equals("")||et_newpassword.getText().toString().equals("")||et_newpassword1.getText().toString().equals("")){
                    ToastUtils.showShort("请完善信息");
                    return;
                }
                if(!et_newpassword.getText().toString().equals(et_newpassword1.getText().toString())){
                    ToastUtils.showShort("新密码两次输入不一致");
                    return;
                }


                JSONObject jsonObject=new JSONObject();
                try {
                    jsonObject.put("uid", LoginUtils.getLoginConfig(XiugaiMimaActivity.this).getuid());
                    jsonObject.put("oldpassword",et_lastpassword.getText().toString());
                    jsonObject.put("newpassword",et_newpassword.getText().toString());
                    jsonObject.put("re_newpassword",et_newpassword1.getText().toString());
                }catch (Exception e){

                }
                ZjbjManager tzsbManager=new ZjbjManager();


                tzsbManager.userChangePassword(jsonObject.toString(), MainUtils.getToken(XiugaiMimaActivity.this), mRetrofit, new BaseSubscriber() {
                    @Override
                    public void onError(ExceptionHandle.ResponeThrowable e) {

                    }

                    @Override
                    public void onSuccess(JSONObject jsonObject) throws JSONException {
                        RequestBean requestBean= FastJsonTools.getBean(jsonObject.toString(),RequestBean.class);
                        if(requestBean.getCode().equals("0")){
                            ToastUtils.showShort("密码修改成功");
                            finish();
                        }else {
                            ToastUtils.showShort(requestBean.getInfo());
                        }
                    }
                });


            }
        });
    }
}
